﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace FluentUnity.Conventions
{
    public class InterfaceScanConvention
    {
        public InterfaceScanConvention()
        {
            AssemblyConditions = new List<Func<Assembly, bool>>();
            TypeConditions = new List<Func<Type, bool>>();
        }

        public void WhereType(Func<Type, bool> condition)
        {
            TypeConditions.Add(condition);
        }

        public void WhereAssembly(Func<Assembly, bool> condition)
        {
            AssemblyConditions.Add(condition);
        }

        private List<Func<Assembly, bool>> AssemblyConditions { get; set; }
        private List<Func<Type, bool>> TypeConditions { get; set; }

        internal IEnumerable<Type> Scan()
        {
            var assemblies = UnityConvention.Current.Assemblies.ApplyConditions(AssemblyConditions);

            return assemblies.SelectMany(a => a.GetTypes().Where(t => t.IsInterface)).ApplyConditions(TypeConditions);
        }
    }
}